package com.nit.controller;

import com.alibaba.fastjson.JSONObject;
import com.nit.dto.GoodsDTO;
import com.nit.dto.StockInDTO;
import com.nit.dto.utils.OpResultDTO;
import com.nit.dto.utils.TableRequestDTO;
import com.nit.dto.utils.TableResponseDTO;
import com.nit.service.StockInService;
import com.nit.utils.JWTUtil;
import com.nit.utils.SystemConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping(value = "/api/stockIn")
public class StockInController {
    @Resource
    private StockInService stockInService;
    private final Logger logger = LoggerFactory.getLogger(StockInController.class);

    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public TableResponseDTO query(@RequestBody TableRequestDTO tableRequestDTO, HttpServletRequest request) {
        TableResponseDTO tableResponseDTO = new TableResponseDTO();
        try {
            Long userId = new Long(request.getHeader("userId"));
            tableResponseDTO = stockInService.query(tableRequestDTO, userId);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableResponseDTO;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestBody StockInDTO stockInDTO,HttpServletRequest request) {
        OpResultDTO opResultDTO = new OpResultDTO(-1L, SystemConstant.OP_FAILURE_OBJ);
        try {
            Long userId = new Long(request.getHeader("userId"));
            opResultDTO.setLongResult(stockInService.add(stockInDTO, userId));
        } catch (Exception e) {
            logger.error(e.toString());
        }
        if (opResultDTO.getLongResult() != -1L) {
            opResultDTO.setObjResult(SystemConstant.OP_SUCCESS_OBJ);
        }
        return opResultDTO;
    }
    }
